#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./paas/common/utils.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.

common utils

Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
import json
from common.log import logger
from common.pxfilter import XssHtml
from release.models import UserOperateRecord

def record_user_operate(app_code, username, operate_type, before_data='', arfter_data='', extra_data=''):
    """
    用户操作记录创建
    @param app_code: app编码
    @param username: 操作人
    @param operate_type: 操作类型
    @param before_data: 操作前数据
    @param arfter_data: 操作后数据
    @param extra_data: 其他数据
    """
    if isinstance(extra_data, dict):
        extra_data = json.dumps(extra_data)
    if not isinstance(extra_data, basestring):
        extra_data = str(extra_data)
    result = UserOperateRecord.objects.create_operate_record(app_code, username, operate_type, before_data, arfter_data, extra_data)
    return result


def html_escape(html, is_json=False):
    """
    Replace special characters "&", "<" and ">" to HTML-safe sequences.
    If the optional flag quote is true, the quotation mark character (")
    is also translated.
    rewrite the cgi method
    @param html: html代码
    @param is_json: 是否为json串（True/False） ，默认为False
    """
    if not is_json:
        html = html.replace('&', '&amp;')
    html = html.replace('<', '&lt;')
    html = html.replace('>', '&gt;')
    if not is_json:
        html = html.replace(' ', '&nbsp;')
        html = html.replace('"', '&quot;')
        html = html.replace("'", '&#39;')
    return html


def url_escape(url):
    url = url.replace('<', '')
    url = url.replace('>', '')
    url = url.replace(' ', '')
    url = url.replace('"', '')
    url = url.replace("'", '')
    return url


def texteditor_escape(str_escape):
    """
    富文本处理
    @param str_escape: 要检测的字符串
    """
    try:
        parser = XssHtml()
        parser.feed(str_escape)
        parser.close()
        return parser.getHtml()
    except Exception as e:
        logger.error(u'js脚本注入检测发生异常，错误信息：%s' % e)
        return str_escape
